<template>
	<view>
		<search ref="search" @search="handleSearch" />
		<view v-show="pageShow">
			<view class="list-card" v-if="!!list.length">
				<uni-card note="true" v-for="(item, index) in list" :key="index" :title="'盘点单号：' + item.checkNo">
					<view class="item">
						<view class="item-content">
							<label>状态：</label>
							<text class="status" :class="{warning: !item.checkResult}">{{checkStateLabel(item.checkState) || '-'}}</text>
						</view>
						<view class="item-content">
							<label>货主代码：</label>
							<text>{{item.ownerCode || '-'}}</text>
						</view>
					</view>
					<view class="item">
						<view class="item-content">
							<label>盘点类型：</label>
							<text>{{item.checkTypeName || '-'}}</text>
						</view>
						<view class="item-content">
							<label>盘点日期：</label>
							<text>{{item.checkDate ? $formatter.formatDate(new Date(item.checkDate)) : '-'}}</text>
						</view>
					</view>
					<template v-slot:footer>
						<view style="display: flex; justify-content: flex-end; height: 56rpx; margin-top: -10rpx;">
							<view style="width: auto;">
								<button size="mini" v-if="item.checkState === '200' || item.checkState === '250'" @click="initial(item)">初盘</button>
								<button size="mini" v-if="item.checkState >= '300'" @click="initialDetail(item.id)">初盘明细</button>
							</view>
							<view style="width: auto;">
								<button style="margin-left: 40rpx;" type="primary" size="mini" v-if="item.checkState === '400' || item.checkState === '450'"
								 @click="replay(item)">复盘</button>
								<button style="margin-left: 40rpx;" type="primary" size="mini" v-if="item.checkState === '500'" @click="replayDetail(item.id)">复盘明细</button>
							</view>
						</view>
					</template>
				</uni-card>
				<uni-load-more :status="loadMoreStatus"></uni-load-more>
			</view>
			<no-data :visible="!list.length" title="暂无数据" :fullscreen="true"></no-data>
		</view>
	</view>
</template>

<script>
	import search from './search.vue';
	import uniCard from '@/components/uni-card/uni-card.vue';
	import loadMore from '@/components/uni-load-more/uni-load-more.vue';
	import noData from '@/components/no-data/no-data.vue';
	import {
		checkState as checkStateDict
	} from './dict.js';

	export default {
		components: {
			search,
			uniCard,
			loadMore,
			noData
		},
		data() {
			return {
				pageShow: false,
				params: {
					limit: global.$limit,
					page: 1,
					warehouseId: this.$storage.getWarehouseId()
				},
				list: [],
				loadMoreStatus: global.$loadMoreStatus.more
			}
		},
		onShow() {
			if (global.$refresh) {
				global.$refresh = false;
				this.load();
			}
		},
		onLoad() {
			this.load();
		},
		onNavigationBarButtonTap() {
			this.$refs.search.open(Object.assign({}, this.params));
		},
		onPullDownRefresh() {
			this.params.page = 1;
			this.fetch();
		},
		onReachBottom() {
			if (this.loadMoreStatus === global.$loadMoreStatus.more) {
				this.loadMoreStatus = global.$loadMoreStatus.loading;
				this.params.page++;
				this.fetch();
			}
		},
		methods: {
			checkStateLabel(value) {
				const _checkState = checkStateDict.find(v => v.value === value);
				if (!_checkState) return null;
				return _checkState.label;
			},
			load() {
				uni.showLoading({
					title: '加载中'
				});
				this.fetch();
			},
			handleSearch(param) {
				this.params = Object.assign({}, {
					limit: global.$limit,
					page: 1,
					warehouseId: this.$storage.getWarehouseId()
				}, param);
				this.load();
			},
			fetch() {
				this.$api.stockCheck.list(this.params).then(res => {
					this.params.page === 1 && uni.pageScrollTo({
						scrollTop: 0,
						duration: 300
					});
					this.list = this.params.page === 1 ? [...res.data] : [...this.list, ...res.data];
					this.loadMoreStatus = this.$pageHelper.lastPage(this.params.limit, this.params.page, res.count) ?
						global.$loadMoreStatus.noMore : global.$loadMoreStatus.more;
					this.pageShow = true;
					uni.hideLoading();
					uni.stopPullDownRefresh();
				}, () => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				});
			},
			initial(stock) {
				uni.navigateTo({
					url: `/pages/stockCheck/initial?id=${stock.id}&checkNo=${stock.checkNo}`
				});
			},
			initialDetail(id) {
				uni.navigateTo({
					url: `/pages/stockCheck/initialDetail?id=${id}`
				});
			},
			replay(stock) {
				uni.navigateTo({
					url: `/pages/stockCheck/replay?id=${stock.id}&checkNo=${stock.checkNo}`
				});
			},
			replayDetail(id) {
				uni.navigateTo({
					url: `/pages/stockCheck/replayDetail?id=${id}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-card /deep/ .uni-card {
		margin: 0 0 30rpx 0;
	}

	.list-card /deep/ .uni-card .uni-card__header {
		padding: 10rpx 24rpx;
	}

	.uni-load-more {
		margin-top: -30rpx;
	}
</style>
